package com.sf.fy.uav.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sf.fy.uav.common.awt.JwtUtil;
import com.sf.fy.uav.common.enums.*;
import com.sf.fy.uav.common.request.operate.CouponIntroductionReq;
import com.sf.fy.uav.common.response.operational.*;
import com.sf.fy.uav.common.response.order.CouponPriceResponse;
import com.sf.fy.uav.common.utils.PhoneEncoderUtil;
import com.sf.fy.uav.common.utils.Rs;
import com.sf.fy.uav.controller.CouponActivityController;
import com.sf.fy.uav.entity.Coupon;
import com.sf.fy.uav.entity.CouponIntroductionUser;
import com.sf.fy.uav.entity.UserCoupon;
import com.sf.fy.uav.mapper.CouponIntroductionUserMapper;
import com.sf.fy.uav.mapper.CouponMapper;
import com.sf.fy.uav.mapper.UserCouponMapper;
import com.sf.fy.uav.rest.CouponRest;
import com.sf.fy.uav.service.IUserCouponService;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author LTQ
 * @since 2023-10-07
 */
@Service
public class UserCouponServiceImpl extends ServiceImpl<UserCouponMapper, UserCoupon> implements IUserCouponService {

    @Autowired
    private UserCouponMapper userCouponMapper;

    @Autowired
    private CouponMapper couponMapper;

    @Autowired
    private CouponActivityController couponActivityController;

    @Autowired
    private CouponRest couponRest;
    @Autowired
    private CouponIntroductionUserMapper couponIntroductionUserMapper;

    @Autowired
    private PhoneEncoderUtil phoneEncoderUtil;
    private final static Integer CONSTANT = 1;
    private final static Integer CONSTANT_NUM = 0;
    private final static Integer MAX_NUM = 10;

    private final static Byte COUPON_PAST_DUE_STATUS = 3;

    private final static String COUPON_NUM_CHECK = "^[0-9]*$";
    //    private final static String PHONE_CHECK = "^1[345789]\\d{9}$";
    private final static String PHONE_NUMBER_LIST = "^[0-9,]+$";


    @Override
    public UserCouponRsResponse getAvailableCoupons(Integer page, Integer size, BigDecimal price, Integer isMustStart) {
        /**
         * 1.未使用 且
         * 2.未过期 且
         * 3.已开始 且
         * 4.无门槛 或 【有门槛且达到门槛金额】
         *
         */
        List<UserCouponResponse> userCouponResponses = new ArrayList<>();
        String userId = JwtUtil.getUserId();
        QueryWrapper<UserCoupon> userCouponQueryWrapper = new QueryWrapper<>();
        userCouponQueryWrapper.eq("user_id", userId);
        //未使用
        userCouponQueryWrapper.eq("status", UserCouponStatusEnum.NOT_USED.getValue());
        //未过期
        userCouponQueryWrapper.gt("expire_time", new Date());
        userCouponQueryWrapper.orderByDesc("update_time");
        if (isMustStart.equals(1)) {
            //已开始
            userCouponQueryWrapper.lt("effective_time", new Date());
        }
        if (!ObjectUtils.isEmpty(price)) {
            //无门槛或者【有门槛且达到门槛金额】
            userCouponQueryWrapper.and(wrapper -> wrapper.and(wr -> wr.and(w -> w.eq("threshold", ThresholdEnum.THRESHOLD.getValue())).and(w -> w.le("threshold_amount", price))).or(wr -> wr.eq("threshold", ThresholdEnum.NO_THRESHOLD.getValue())));
        }
        Page<UserCoupon> userCouponPage = userCouponMapper.selectPage(new Page<>(page, size), userCouponQueryWrapper);
        List<UserCoupon> userCoupons = userCouponPage.getRecords();
        if (!CollectionUtils.isEmpty(userCoupons)) {
            List<String> couponIds = userCoupons.stream().map(userCoupon -> userCoupon.getCouponId()).collect(Collectors.toList());
            List<Coupon> coupons = couponMapper.selectBatchIds(couponIds);
            for (UserCoupon userCoupon : userCoupons) {
                UserCouponResponse userCouponResponse = new UserCouponResponse();
                userCouponResponse.setFailureTime(userCoupon.getExpireTime());
                userCouponResponse.setEffectiveTime(userCoupon.getEffectiveTime());
                userCouponResponse.setUpdateTime(userCoupon.getUpdateTime());
                userCouponResponse.setId(userCoupon.getId());
                for (Coupon coupon : coupons) {
                    if (userCoupon.getCouponId().equals(coupon.getId())) {

                        userCouponResponse.setCouponName(coupon.getCouponName());
                        userCouponResponse.setCouponType(coupon.getCouponType());
                        userCouponResponse.setDiscount(coupon.getDiscount().doubleValue());
                        userCouponResponse.setThreshold(coupon.getThreshold());
                        userCouponResponse.setThresholdAmount(coupon.getThresholdAmount());
                        String description = coupon.getDescription();
                        if (!ObjectUtils.isEmpty(description)) {
                            userCouponResponse.setDescription(description.replaceAll(",", ""));
                        }
                    }
                }
                userCouponResponses.add(userCouponResponse);
            }
        }
        Long total = userCouponMapper.selectCount(userCouponQueryWrapper);
        UserCouponRsResponse userCouponRsResponse = new UserCouponRsResponse();
        userCouponResponses = userCouponResponses.stream().sorted(Comparator.comparingDouble(UserCouponResponse::getDiscount).thenComparing(UserCouponResponse::getFailureTime).thenComparing(UserCouponResponse::getUpdateTime, Comparator.reverseOrder())).collect(Collectors.toList());
        userCouponRsResponse.setUserCouponResponseList(userCouponResponses);
        userCouponRsResponse.setTotal(total);
        return userCouponRsResponse;
    }

    @Override
    public UserCouponRsResponse getUnavailableCoupons(Integer page, Integer size, BigDecimal price) {
        /**
         * 1.未使用 且
         * 2.未过期 且
         * 3.未开始 或 【有门槛但是没有达到门槛金额】
         */
        List<UserCouponResponse> userCouponResponses = new ArrayList<>();
        String userId = JwtUtil.getUserId();
        QueryWrapper<UserCoupon> userCouponQueryWrapper = new QueryWrapper<>();
        userCouponQueryWrapper.eq("user_id", userId);
        //未使用
        userCouponQueryWrapper.eq("status", UserCouponStatusEnum.NOT_USED.getValue());
        //未过期
        userCouponQueryWrapper.gt("expire_time", new Date());
        userCouponQueryWrapper.orderByDesc("update_time");
        //未开始或者【有门槛但是没有达到门槛金额】
        userCouponQueryWrapper.and(wrapper -> wrapper.and(wr -> wr.and(w -> w.eq("threshold", ThresholdEnum.THRESHOLD.getValue())).and(w -> w.gt("threshold_amount", price))).or(wr -> wr.gt("effective_time", new Date())));
        Page<UserCoupon> userCouponPage = userCouponMapper.selectPage(new Page<>(page, size), userCouponQueryWrapper);
        List<UserCoupon> userCoupons = userCouponPage.getRecords();
        if (!CollectionUtils.isEmpty(userCoupons)) {
            List<String> couponIds = userCoupons.stream().map(userCoupon -> userCoupon.getCouponId()).collect(Collectors.toList());
            List<Coupon> coupons = couponMapper.selectBatchIds(couponIds);
            for (UserCoupon userCoupon : userCoupons) {
                UserCouponResponse userCouponResponse = new UserCouponResponse();
                Date effectiveTime = userCoupon.getEffectiveTime();
                Byte threshold = userCoupon.getThreshold();
                BigDecimal thresholdAmount = userCoupon.getThresholdAmount();
                Integer unavailableReason = 0;
                if (new Date().before(effectiveTime)) {
                    unavailableReason = 1;
                }
                if (threshold.equals(ThresholdEnum.THRESHOLD.getValue())) {
                    if (price.compareTo(thresholdAmount) < 0) {
                        if (unavailableReason.equals(1)) {
                            unavailableReason = 3;
                        } else {
                            unavailableReason = 2;
                        }

                    }
                }
                if (!unavailableReason.equals(0)) {
                    userCouponResponse.setUnavailableReason(unavailableReason);
                }
                userCouponResponse.setFailureTime(userCoupon.getExpireTime());
                userCouponResponse.setEffectiveTime(userCoupon.getEffectiveTime());
                userCouponResponse.setId(userCoupon.getId());
                userCouponResponse.setUpdateTime(userCoupon.getUpdateTime());
                for (Coupon coupon : coupons) {
                    if (userCoupon.getCouponId().equals(coupon.getId())) {

                        userCouponResponse.setCouponName(coupon.getCouponName());
                        userCouponResponse.setCouponType(coupon.getCouponType());
                        userCouponResponse.setDiscount(coupon.getDiscount().doubleValue());
                        userCouponResponse.setThreshold(coupon.getThreshold());
                        userCouponResponse.setThresholdAmount(coupon.getThresholdAmount());
                        String description = coupon.getDescription();
                        if (!ObjectUtils.isEmpty(description)) {
                            userCouponResponse.setDescription(description.replaceAll(",", ""));
                        }
                    }
                }
                userCouponResponses.add(userCouponResponse);
            }
        }
        Long total = userCouponMapper.selectCount(userCouponQueryWrapper);
        UserCouponRsResponse userCouponRsResponse = new UserCouponRsResponse();
        userCouponResponses = userCouponResponses.stream().sorted(Comparator.comparingDouble(UserCouponResponse::getDiscount).thenComparing(UserCouponResponse::getFailureTime).thenComparing(UserCouponResponse::getUpdateTime, Comparator.reverseOrder())).collect(Collectors.toList());
        userCouponRsResponse.setUserCouponResponseList(userCouponResponses);
        userCouponRsResponse.setTotal(total);
        return userCouponRsResponse;
    }

    @Override
    public Rs<CouponIntroductionResult> introduction(CouponIntroductionReq couponIntroductionReq) {
        Coupon coupon = couponMapper.selectById(couponIntroductionReq.getCouponId());
        if (ObjectUtils.isEmpty(coupon)) {
            throw new RuntimeException("查询此优惠券为空!");
        }

        Integer effectiveDays = coupon.getEffectiveDays();
        if (ObjectUtils.isEmpty(effectiveDays)) {
            if (coupon.getFailureTime().getTime() < System.currentTimeMillis()) {
                throw new RuntimeException("该优惠券已过期不能发放!");
            }
        }
        if (StringUtils.isBlank(couponIntroductionReq.getOperator())) {
            throw new RuntimeException("操作人不能为空!");
        }

        if (!couponIntroductionReq.getCouponNum().toString().matches(COUPON_NUM_CHECK)) {
            throw new RuntimeException("仅支持输入正整数数字");
        }
        if (couponIntroductionReq.getCouponNum() < CONSTANT || couponIntroductionReq.getCouponNum() > MAX_NUM) {
            throw new RuntimeException("发券数量范围:[1,10]");
        }
        if (!couponIntroductionReq.getPhoneNumberList().matches(PHONE_NUMBER_LIST)) {
            throw new RuntimeException("仅支持输入数字和英文逗号");
        }
//        for (String phone: couponIntroductionReq.getPhoneArray()) {
//            if(!phone.matches(PHONE_CHECK)){
//                throw new RuntimeException("手机号格式错误");
//            }
//        }
        if (coupon.getAlreadyRecNum() >= coupon.getTotalQuantity()) {
            throw new RuntimeException("发券数量不能超过该优惠券发行量");
        }
        if (couponIntroductionReq.getCouponNum() * couponIntroductionReq.getUserIds().size() + coupon.getAlreadyRecNum() > coupon.getTotalQuantity()) {
            throw new RuntimeException("发券数量不能超过该优惠券发行量");
        }
        List<UserCoupon> userCouponList = new ArrayList<>();
        //3张优惠券 发放5个用户id,每个用户都发3张,带上同一类型的优惠券id
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        if (ObjectUtils.isNotEmpty(couponIntroductionReq.getCouponNum()) && couponIntroductionReq.getCouponNum() > CONSTANT_NUM) {
            for (int i = 0; i < couponIntroductionReq.getCouponNum(); i++) {
                if (!CollectionUtils.isEmpty(couponIntroductionReq.getUserIds())) {
                    for (String userId : couponIntroductionReq.getUserIds()) {
                        UserCoupon userCoupon = new UserCoupon();
                        String[] uuid = UUID.randomUUID().toString().split("-");
                        userCoupon.setId(uuid[uuid.length - 2] + uuid[uuid.length - 1]);
                        userCoupon.setStatus(CONSTANT);
                        userCoupon.setCouponId(couponIntroductionReq.getCouponId());
                        userCoupon.setUserId(userId);
                        userCoupon.setThreshold(coupon.getThreshold());
                        userCoupon.setThresholdAmount(coupon.getThresholdAmount());
                        userCoupon.setSource(UserCouponSourceEnum.PASSIVE_ACQUISITION.getValue());
                        userCoupon.setOperator(couponIntroductionReq.getOperator());
                        if (!ObjectUtils.isEmpty(effectiveDays)) {

                            userCoupon.setEffectiveTime(date);
                            //用户领取优惠券当日起+n-1天有效，n天截止时间为23:59:59
                            Calendar calendar = Calendar.getInstance();
                            calendar.setTime(date);
                            calendar.add(Calendar.DAY_OF_YEAR, effectiveDays - 1);
                            try {
                                userCoupon.setExpireTime(sdf.parse(sdf.format(calendar.getTime()).substring(0, 10) + " 23:59:59"));
                            } catch (ParseException e) {
                                throw new RuntimeException(e);
                            }
                        } else {
                            userCoupon.setEffectiveTime(coupon.getEffectiveTime());
                            userCoupon.setExpireTime(coupon.getFailureTime());
                        }

                        userCoupon.setCreateTime(date);
                        userCoupon.setUpdateTime(date);
                        userCouponList.add(userCoupon);
                    }
                }
            }
        }
        if (CollectionUtils.isEmpty(userCouponList)) {
            CouponIntroductionResult couponIntroductionRes = new CouponIntroductionResult();
            couponIntroductionRes.setIntroductionUserNum(couponIntroductionReq.getUserIds().size());
            couponIntroductionRes.setUnregisteredPhone(couponIntroductionReq.getPhoneSet());
            interIssueFailUser(couponIntroductionReq.getCouponId(), couponIntroductionReq.getPhoneSet());
            return Rs.success(couponIntroductionRes);
        }
        int i = userCouponMapper.insertBatchSomeColumn(userCouponList);
        if (i > CONSTANT_NUM) {
            coupon.setAlreadyRecNum(coupon.getAlreadyRecNum() + userCouponList.size());
            coupon.setIssueStatus(CouponIssueStatusEnum.ISSUED.getValue());
            couponMapper.updateById(coupon);
            //下架库存不足的优惠券活动
            couponActivityController.downByStockNotEnough(coupon.getId(), coupon.getTotalQuantity() - coupon.getAlreadyRecNum());

            CouponIntroductionResult couponIntroductionRest = new CouponIntroductionResult();
            couponIntroductionRest.setIntroductionUserNum(couponIntroductionReq.getUserIds().size());
            couponIntroductionRest.setUnregisteredPhone(couponIntroductionReq.getPhoneSet());
            interIssueFailUser(couponIntroductionReq.getCouponId(), couponIntroductionReq.getPhoneSet());
            return Rs.success(couponIntroductionRest);
        }
        return null;
    }

    @Override
    public ExpireOrUsedUserRsCouponResponse getExpireOrUsedCoupons(Integer page, Integer size) {
        List<ExpireOrUsedUserCouponResponse> userCouponResponses = new ArrayList<>();
        String userId = JwtUtil.getUserId();
        QueryWrapper<UserCoupon> userCouponQueryWrapper = new QueryWrapper<>();
        userCouponQueryWrapper.eq("user_id", userId);
        userCouponQueryWrapper.orderByDesc("update_time");
        userCouponQueryWrapper.and(wrapper -> wrapper.lt("expire_time", new Date()).or(wr -> wr.eq("status", UserCouponStatusEnum.USED.getValue())));
        Page<UserCoupon> userCouponPage = new Page<>(page, size);
        Page<UserCoupon> userCouponPage1 = userCouponMapper.selectPage(userCouponPage, userCouponQueryWrapper);
        List<UserCoupon> userCoupons = userCouponPage1.getRecords();
        if (!CollectionUtils.isEmpty(userCoupons)) {
            List<String> couponIds = userCoupons.stream().map(userCoupon -> userCoupon.getCouponId()).collect(Collectors.toList());
            List<Coupon> coupons = couponMapper.selectBatchIds(couponIds);
            for (UserCoupon userCoupon : userCoupons) {
                ExpireOrUsedUserCouponResponse userCouponResponse = new ExpireOrUsedUserCouponResponse();
                Integer status = userCoupon.getStatus();
                Date expireTime = userCoupon.getExpireTime();
                if (status.equals(UserCouponStatusEnum.USED.getValue())) {
                    userCouponResponse.setUnavailableReason(1);
                } else {
                    if (expireTime.before(new Date())) {
                        userCouponResponse.setUnavailableReason(2);
                    }
                }
                userCouponResponse.setFailureTime(expireTime);
                userCouponResponse.setEffectiveTime(userCoupon.getEffectiveTime());
                userCouponResponse.setId(userCoupon.getId());
                for (Coupon coupon : coupons) {
                    if (userCoupon.getCouponId().equals(coupon.getId())) {

                        userCouponResponse.setCouponName(coupon.getCouponName());
                        userCouponResponse.setCouponType(coupon.getCouponType());
                        userCouponResponse.setDiscount(coupon.getDiscount());
                        userCouponResponse.setThreshold(coupon.getThreshold());
                        userCouponResponse.setThresholdAmount(coupon.getThresholdAmount());

                        String description = coupon.getDescription();
                        if (!ObjectUtils.isEmpty(description)) {
                            userCouponResponse.setDescription(description.replaceAll(",", ""));
                        }
                    }
                }
                userCouponResponses.add(userCouponResponse);
            }
        }
        Long total = userCouponMapper.selectCount(userCouponQueryWrapper);
        ExpireOrUsedUserRsCouponResponse expireOrUsedUserRsCouponResponse = new ExpireOrUsedUserRsCouponResponse();
        expireOrUsedUserRsCouponResponse.setExpireOrUsedUserCouponResponseList(userCouponResponses);
        expireOrUsedUserRsCouponResponse.setTotal(total);
        return expireOrUsedUserRsCouponResponse;
    }

    @Override
    public CouponPriceResponse calculatePrice(BigDecimal price, String id) {
        CouponPriceResponse couponPriceResponse = new CouponPriceResponse();
        UserCoupon userCoupon = this.getEffectiveCoupon(price, id);
        BigDecimal finalPrice = price;
        if (ObjectUtils.isEmpty(userCoupon)) {
            throw new RuntimeException("优惠券已失效，请使用其他优惠券");
        }
        String couponId = userCoupon.getCouponId();
        if (!ObjectUtils.isEmpty(couponId)) {
            Coupon coupon = couponMapper.selectById(couponId);
            if (ObjectUtils.isEmpty(coupon)) {
                throw new RuntimeException("优惠券已失效，请使用其他优惠券");
            }
            Byte couponType = coupon.getCouponType();
            if (couponType.equals(CouponTypeEnum.UNDER_WAY.getValue())) {
                BigDecimal discount = coupon.getDiscount();
                couponPriceResponse.setDiscount(discount);
                finalPrice = price.multiply(discount).divide(new BigDecimal(10));
            }
            couponPriceResponse.setCouponType(couponType);
        }
        finalPrice = finalPrice.setScale(2, RoundingMode.HALF_UP);
        couponPriceResponse.setPrice(finalPrice);
        couponPriceResponse.setReductionPrice(price.subtract(finalPrice).setScale(2, RoundingMode.HALF_UP));

        return couponPriceResponse;
    }


    @Override
    public UserCoupon getEffectiveCoupon(BigDecimal price, String id) {
        String userId = JwtUtil.getUserId();
        QueryWrapper<UserCoupon> userCouponQueryWrapper = new QueryWrapper<>();
        userCouponQueryWrapper.eq("user_id", userId);
        //未使用
        userCouponQueryWrapper.eq("status", UserCouponStatusEnum.NOT_USED.getValue());
        //未过期
        userCouponQueryWrapper.gt("expire_time", new Date());
        //已开始
        userCouponQueryWrapper.lt("effective_time", new Date());
        userCouponQueryWrapper.eq("id", id);
        return userCouponMapper.selectOne(userCouponQueryWrapper);
    }

    private void interIssueFailUser(String couponId, Set<String> phoneNum) {
        List<CouponIntroductionUser> couponIntroductionUsers = new ArrayList<>();
        if (!CollectionUtils.isEmpty(phoneNum)) {
            for (String phone : phoneNum) {
                CouponIntroductionUser couponIntroductionUser = new CouponIntroductionUser();
                couponIntroductionUser.setCouponId(couponId);
                couponIntroductionUser.setPhone(phoneEncoderUtil.encryptString(phone));
                couponIntroductionUser.setCreateTime(new Date());
                couponIntroductionUser.setUpdateTime(new Date());
                couponIntroductionUsers.add(couponIntroductionUser);
            }
            couponIntroductionUserMapper.insertBatchSomeColumn(couponIntroductionUsers);
        }
    }

    @Scheduled(cron = "30 * * * * ?")
    public void autoLoadAndUnload() {
        //0:待领取 1:未使用 2:已使用
        List<UserCoupon> userCouponList = userCouponMapper.selectList(new QueryWrapper<UserCoupon>().and(wrapper -> wrapper.eq("status", UserCouponStatusEnum.NOT_RECEIVE.getValue())).or(wrapper -> wrapper.eq("status", UserCouponStatusEnum.NOT_USED.getValue())));
        ArrayList<String> userCouponNotReceiveIds = new ArrayList<>();
        ArrayList<String> userCouponNotUsedIds = new ArrayList<>();

//        ArrayList<String> userCouponNotReceiveCouponIds = new ArrayList<>();
//        ArrayList<String> userCouponNotUsedCouponIds = new ArrayList<>();
        for (UserCoupon userCoupon : userCouponList) {
            if (UserCouponStatusEnum.NOT_RECEIVE.getValue().equals(userCoupon.getStatus()) && System.currentTimeMillis() >= userCoupon.getExpireTime().getTime()) {
                userCouponNotReceiveIds.add(userCoupon.getId());
//                userCouponNotReceiveCouponIds.add(userCoupon.getCouponId());
            }
            if (UserCouponStatusEnum.NOT_USED.getValue().equals(userCoupon.getStatus()) && System.currentTimeMillis() >= userCoupon.getExpireTime().getTime()) {
                userCouponNotUsedIds.add(userCoupon.getId());
//                userCouponNotUsedCouponIds.add(userCoupon.getCouponId());
            }
        }
        UserCoupon userCouponNotReceive = new UserCoupon();
        userCouponNotReceive.setStatus(UserCouponStatusEnum.EXPIRED.getValue());
        if (!CollectionUtils.isEmpty(userCouponNotReceiveIds)) {
            userCouponMapper.update(userCouponNotReceive, new UpdateWrapper<UserCoupon>().in("id", userCouponNotReceiveIds));
        }
//
//        Coupon couponNotReceive = new Coupon();
//        couponNotReceive.setStatus(COUPON_PAST_DUE_STATUS);
//        if (!CollectionUtils.isEmpty(userCouponNotReceiveCouponIds)) {
//            couponMapper.update(couponNotReceive, new UpdateWrapper<Coupon>().eq("del", false).in("id", userCouponNotReceiveCouponIds));
//        }


        UserCoupon userCouponNotUsed = new UserCoupon();
        userCouponNotUsed.setStatus(UserCouponStatusEnum.EXPIRED.getValue());
        if (!CollectionUtils.isEmpty(userCouponNotUsedIds)) {
            userCouponMapper.update(userCouponNotUsed, new UpdateWrapper<UserCoupon>().in("id", userCouponNotUsedIds));
        }
//        Coupon couponNotUsed = new Coupon();
//        couponNotUsed.setStatus(CouponStatusEnum.ENDED.getValue());
//        if (!CollectionUtils.isEmpty(userCouponNotUsedCouponIds)) {
//            couponMapper.update(couponNotUsed, new UpdateWrapper<Coupon>().eq("del", false).in("id", userCouponNotUsedCouponIds));
//            //需要下架关联的优惠券活动
//            couponActivityController.downByCouponIds(userCouponNotUsedCouponIds);
//        }

    }
}
